/**
 * @author JackGao
 *			email:jackgao.china@gmail.com
 *  date：2016-6-26
 * 
 */
package cn.cuit.ds.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import cn.cuit.ds.domain.Address;
import cn.cuit.ds.domain.Goods;
import cn.cuit.ds.domain.OrderDeatil;
import cn.cuit.ds.domain.Orders;
import cn.cuit.ds.domain.Shopper;
import cn.cuit.ds.domain.User;
import cn.cuit.ds.service.IUserService;
import cn.cuit.ds.service.impl.UserService;
import cn.cuit.ds.utils.PageBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class UserAction extends ActionSupport {

	// 实体
	User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	Address address;

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	String currentPage;
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	
	String from;
	public String getFrom() {
		return from;
	}
	public void setFrom(String from) {
		this.from = from;
	}
	String to;
	public String getTo() {
		return to;
	}
	public void setTo(String to) {
		this.to = to;
	}
	
	
	
	
	Goods goods;
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	public Goods getGoods() {
		return goods;
	}
	Orders order;
	public void setOrder(Orders order) {
		this.order = order;
	}
	public Orders getOrder() {
		return order;
	}
	// service
	@Resource(name="UserService")
	UserService userService ;
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public String login() {
		User login = userService.login(user);
		if (login == null) {
			super.addFieldError("err", "please check your password");
			return "err";
		}
		ActionContext.getContext().getSession().put("userInfo", login);
		return "loginSuccess";
	}

	public String register() {
		userService.regist(user);
		return "register";
	}
	
	/**
	 * 查看所有地址
	 * @return
	 */
	public String viewAllAddress(){
		User userInfo = (User) ActionContext.getContext().getSession().get("userInfo");
		List<Address> allAddress = userService.getAllAddress(userInfo.getUserId());
		ServletActionContext.getRequest().setAttribute("allAddress", allAddress);
		return	"viewAllAddress";
	}

	public String addAddress(){
		User userInfo = (User) ActionContext.getContext().getSession().get("userInfo");
		address.setUserId(userInfo.getUserId());
		userService.addAddress(address);
		return "addAddress";
	}
	
	/**
	 * 根据表单提交的内容更新地址
	 * @return
	 */
	public String updateAddress(){
		userService.updateAddress(address);
		return "updateAddress";
	}
	
	/**
	 * 显示要更新的地址的详细信息
	 * @return
	 */
	public String viewOneAddress(){
		Address addressById = userService.getAddressById(address.getAddressId());
		ServletActionContext.getRequest().setAttribute("address", addressById);
		return "viewOneAddress";
	}
	
	/**
	 * 删除地址
	 * @return
	 */
	public String delete(){
		userService.deleteAddress(address.getAddressId());
		return "delete";
	}
	
	/**
	 * 订单详情
	 * @return
	 */
	public String viewDetail(){
		User userInfo = (User) ActionContext.getContext().getSession().get("userInfo");
		List<OrderDeatil> detailById = userService.getAllOrderDetailById(userInfo.getUserId());
		ServletActionContext.getRequest().setAttribute("orderDetail", detailById);
		return "viewDetail";
		
	}
	
	/**
	 * 获取所有商品
	 * @return
	 */
	public String viewAllGoods(){
//		List<Goods> allGoods = userService.getAllGoods();
//		ServletActionContext.getRequest().setAttribute("allGoods",allGoods);
		// 判断
		if (currentPage == null || "".equals(currentPage.trim())){
			currentPage = "1";  	// 第一次访问，设置当前页为1;
		}
		// 转换
		int curr = Integer.parseInt(currentPage);
		//2. 创建PageBean对象，设置当前页参数； 传入service方法参数
		PageBean<Goods> pageBean = new PageBean<Goods>();
		pageBean.setCurrentPage(curr);
		
		//3. 调用service  
		userService.getAllGoods(pageBean);    // 【pageBean已经被dao填充了数据】
		
		//4. 保存pageBean对象，到request域中
		ServletActionContext.getRequest().setAttribute("pageBean", pageBean);
		return "viewAllGoods";
	}
	/**
	 * 购物车界面
	 * @return
	 */
	
	public String gwc(){
		List<String> car = (List<String>) ActionContext.getContext().getSession().get("car");
		List<Goods> list = userService.getAllGoodsByIds(car);
		//System.out.println(list);
		//ServletActionContext.getRequest().setAttribute("gwc", list);
		ActionContext.getContext().getSession().put("gwc", list);
		return "gwc";
	}
	
	
	/**
	 * 把商品加入到购物车
	 * @return
	 */
	public String addToCar(){
		
		//先判断用户是否登陆
		User user = (User) ActionContext.getContext().getSession().get("userInfo");
		if (user == null) {
			return "login";
		}
		
		// 把商品添加到session域中
		List<String> car = (List<String>) ActionContext.getContext().getSession().get("car");
		if (car==null) {
			car = new ArrayList<String>();
		}
		car.add(goods.getGoodsId());
		ActionContext.getContext().getSession().put("car", car);
		
		
		// 把麦家Id添加到session域中
		HashSet<String> set = (HashSet<String>) ActionContext.getContext().getSession().get("set");
		if (set==null) {
			set = new HashSet<String>();
		}
		set.add(goods.getShoperId());
		ActionContext.getContext().getSession().put("set", set);
		
		return "addToCar";
	}
	/**
	 * 结算
	 * @return
	 */
	public String checkOut(){
		List<Goods> gwc = (List<Goods>) ActionContext.getContext().getSession().get("gwc");
		// 商家的id
		HashSet<String> set = (HashSet<String>) ActionContext.getContext().getSession().get("set");
		
		// 用户id
		User user = (User) ActionContext.getContext().getSession().get("userInfo");
		for (String shoperId : set) {
			double detail2 = 0;
			String orderName="";
			for (Goods good : gwc) {
				if (good.getShoperId().equals(shoperId)) {
					orderName+=good.getGoodsName()+"     ";
					detail2 += Double.valueOf(good.getGoodsPrice());
				}
				userService.updateDatabase(good);
			}
			
			order.setOrderName(orderName);
			order.setDetail2(detail2+"");
			order.setShoperId(shoperId);
			order.setAddressId(order.getAddressId());
			userService.checkout(order);
		}
		 ActionContext.getContext().getSession().remove("car");
		 ActionContext.getContext().getSession().remove("set");
		 ActionContext.getContext().getSession().remove("gwc");
		return "checkOut";
	}
	/**
	 * 结算界面的显示
	 * @return
	 */
	
	public String checkOutView(){
		User user = (User) ActionContext.getContext().getSession().get("userInfo");
		List<Address> add = userService.getAllAddress(user.getUserId());
		ServletActionContext.getRequest().setAttribute("address", add);
		return "checkOutView";
	}
	
	
	
	public String pay(){
		return "pay";
	}
	
	public String logout(){
		ActionContext.getContext().getSession().remove("userInfo");
		return "logout";
	}
	
	public String bydate(){
		User userInfo = (User) ActionContext.getContext().getSession().get("userInfo");
		List<OrderDeatil> detailById = userService.getAllOrderDetailByIdAndTime(userInfo.getUserId(),from,to);
		ServletActionContext.getRequest().setAttribute("orderDetail", detailById);
		return "viewDetail";
	}
	
}
